package com.ml.loan.sys.controller;


import com.ml.loan.common.model.JsonResult;
import com.ml.loan.common.model.PageResult;
import com.ml.loan.sys.model.SysOperRecord;
import com.ml.loan.sys.model.User;
import com.ml.loan.sys.service.SysOperRecordService;
import com.ml.loan.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;

/**
 * <p>
 * 操作日志 前端控制器
 * </p>
 *
 * @author milian
 * @since 2022-08-22
 */
@Controller
@RequestMapping("/sys/operRecord")
public class SysOperRecordController {

    @Autowired
    private SysOperRecordService operRecordService;

    @Autowired
    private UserService userService;


    @RequestMapping()
    public String view() {
        return "system/oper-record.html" ;
    }


    @RequestMapping("/view")
    public String view(@PathParam("id") Integer id, Model model) {
        SysOperRecord record = operRecordService.getById(id);
        if (record != null && record.getUserId() != null) {
            User user = userService.getByUserId(record.getUserId());
            record.setUsername(user.getUsername());
            record.setNickName(user.getNickName());
        }
        model.addAttribute("data", record);
        return "system/oper-record-info.html" ;
    }


    @ResponseBody
    @RequestMapping("/list")
    public PageResult<SysOperRecord> list(Integer page, Integer limit, String startDate, String endDate, String searchKey, String searchValue, Integer userType) {
        return operRecordService.list(page, limit, startDate, endDate, searchKey, searchValue, userType);
    }


    /**
     * 根据id查询操作日志
     */
    @ResponseBody
    @RequestMapping("/get")
    public JsonResult get(Integer id) {
        SysOperRecord record = operRecordService.getById(id);
        return JsonResult.ok().put("data", record);
    }
}
